import json
from base64 import b64encode

import requests
from Crypto.Cipher import AES

"""处理加密过程"""
"""    
function a(a) {  # 返回16位随机字符串
        var d, e, b = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", c = "";
        for (d = 0; a > d; d += 1)
            e = Math.random() * b.length,
            e = Math.floor(e),
            c += b.charAt(e);
        return c
    }
    function b(a, b) {
        var c = CryptoJS.enc.Utf8.parse(b)
          , d = CryptoJS.enc.Utf8.parse("0102030405060708")
          , e = CryptoJS.enc.Utf8.parse(a)
          , f = CryptoJS.AES.encrypt(e, c, {
            iv: d,
            mode: CryptoJS.mode.CBC
        });
        return f.toString()
    }
    function c(a, b, c) {
        var d, e;
        return setMaxDigits(131),
        d = new RSAKeyPair(b,"",c),
        e = encryptedString(d, a)
    }
    function d(d, e, f, g) {  #d:原始数据，e: bsg5l(["流泪", "强"]), f: bsg5l(TH9y.md), g: bsg5l(["爱心", "女孩", "惊恐", "大笑"])
        var h = {}
          , i = a(16);
        return h.encText = b(d, g),
        h.encText = b(h.encText, i),
        h.encSecKey = c(i, e, f),
        h
    }
"""
# d = i1x
e = "010001"
f = "00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7"
g = "0CoJUm6Qyw8W8jud"
i = "tDTwj5nF4BpuxFbP"  # 16位随机数，这里固定


# AES加密模拟
def enc_params(data, key):
    iv = "0102030405060708"
    aes = AES.new(key=key.encode("utf-8"), iv=iv.encode("utf-8"), mode=AES.MODE_CBC)
    data = to_16(data)  # 加密数据需为16的倍数位
    bs = aes.encrypt(data.encode("utf-8"))
    return str(b64encode(bs), "utf-8")


# 将数据补充为16位
def to_16(data):
    pad = 16 - len(data) % 16
    data += chr(pad) * pad
    return data


def get_params(data):
    first = enc_params(data, g)
    second = enc_params(first, i)
    return second


def get_encseckey():
    return "5c41f794670056935e05eb7a66db88e1a857e7f96899ed8167b5b0fdee4455e454ebb89878139d30d23ca63ddb179b72eff74d54e6025657ed8ad984ab01fbc76a5a5e4cc5350e8376125841ba65a78269a2e070aeb7720dbba605cb690f55104f39d57d1130f10a544ad9a66045c9e1b8a2f7ce29e3f36b1e5007f49076ae4b"


# 单个data
data = {
    "params": "8uYltkUdfr6b9S7Zjivj7peJbrMDTlSoTNa6k+DbJQCDKuK9DqRAh46fxTZV8Y++EwcxExwvlsA1kDxBt8iKeijeF5BxlnsPwAdvmxU2/C4LbFu7kcP2kOR0H2mYy+9uJCuKDcR1f4h967311K8JmCPmKb4hOB5Xa9fB3+Hq2kH6bdcIGj88RWnc3Y27Ghr4Cz3DgdsR3bt21MVTlW2gJNdrKwcR/V/bo8c+1EBV5my8cjrngDzexUv/GwthgU30IGXCK6pYGe/7hn5eaviM0y9veWDTdy2pf8R0HbSG/sM=",
    "encSecKey": "ab0e1e08198b0e712e676d42417ee84b1b759ec73244daa66f08ce543ee68044bae57de4d5ab4c17e3c327db1a37c3199906e9502d341fe7e1d8784c0e53a5d585d06c312ad2a29607e178280cae397757fb4f3f66549cb001e16022992df5010cb811bb44760055a44c4e063a98f5b475c8a9ab21343493f993f8902b82c39a"}

"""主程序"""
# 冰河时代
i1x = {"csrf_token": "",
       "cursor": "-1",
       "offset": "0",
       "orderType": "1",
       "pageNo": "1",
       "pageSize": "20",
       "rid": "R_SO_4_1970559933",
       "threadId": "R_SO_4_1970559933"}
# R_SO_4_1970559943
url = "https://music.163.com/weapi/comment/resource/comments/get?csrf_token="
data1 = {"params": get_params(json.dumps(i1x)), "encSecKey": get_encseckey()}
res = requests.post(url, data=data1).json()
print(res)
hot_comments = res["data"]["hotComments"]
for i in hot_comments:
    content = i["content"]
    print(content)
